package com.zhu.controller;

import com.zhu.pojo.Organization;
import com.zhu.pojo.Person;
import com.zhu.pojo.Result;
import com.zhu.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/person")
public class PersonController {
    @Autowired
    private PersonService personService;

    @PostMapping("search")
    public Result search(@RequestBody Person person){
        try{
            List<Person> list = personService.search(person);
            if(list.size()!=0)
                return Result.ok();
            return Result.fail("");
        }catch (Exception e){
            e.printStackTrace();
            return Result.ok();
        }
    }

    @PostMapping("/personadd")
    public void personadd(@RequestBody Person person){
        personService.personadd(person);
    }

    @PostMapping("/getlist")
    public List<Organization> getlist(){
        List<Organization> list= new ArrayList<>();
        list.add(personService.getlist());
        return list;
    }

    @PostMapping("/searchbyname")
    public Person searchByName(@RequestBody String name){
        return personService.searchByName(name);
    }

    @PostMapping("/update")
    public void update(@RequestBody Person person){
        personService.update(person);
    }

    @PostMapping("/searchOne")
    public Person searchOne(@RequestBody String account){
        return personService.searchOne(account);
    }

    @PostMapping("/delete")
    public void delete(@RequestBody String account){
        personService.delete(account);
    }
}
